package com.etc.service.impl;

import com.etc.mapper.IUserDao;
import com.etc.pojo.User;
import com.etc.service.IUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @program: RookieSocialNetworking02
 * @description: 用户业务层实现类
 * @author: 梁伟文
 * @create: 2020-08-14 20:20
 **/
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private IUserDao dao;

    @Override
    public User login(User user) {
        String password = DigestUtils.md5Hex(user.getUserPwd());
        user.setUserPwd(DigestUtils.md5Hex(password + User.SOLT));
        User u = dao.queryUserByNameAndPwd(user);
        System.out.println(u);
        if(u != null && u.getUserStatus() == 1)
            return u;
        return null;
    }

    @Override
    public Boolean register(User user) {
        String password = DigestUtils.md5Hex(user.getUserPwd());
        user.setUserPwd(DigestUtils.md5Hex(password + User.SOLT));
        return dao.saveUser(user) == 0 ? false : true;
    }

    @Override
    public User queryUserByName(String name) {
        return dao.queryUserByName(name);
    }

    @Override
    public User queryUserByTelephone(String phone) {
        return dao.queryUserByTelephone(phone);
    }

    @Override
    public Boolean updateUser(User user) {
        return dao.updateUser(user) == 0 ? false : true;
    }

    @Override
    public PageInfo<User> queryNotAddUser(List<Integer> friendIds, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<User> users = dao.queryNotAddUser(friendIds);
        PageInfo<User> pageInfo = new PageInfo<>(users);
        return pageInfo;
    }

    @Override
    public PageInfo<User> queryNotAddUserByNameOrTel(List<Integer> friendIds, User user, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<User> users = dao.queryNotAddUserByNameOrTel(friendIds,user);
        PageInfo<User> pageInfo = new PageInfo<>(users);
        return pageInfo;
    }

    @Override
    public List<User> getAllUsers() {
        return dao.queryAllUsers();
    }

    @Override
    public List<User> getPageUsers(String userName, int pageNum, int size) {
        PageHelper.startPage(pageNum, size);
        return dao.queryByLike(userName);
    }

    @Override
    public boolean add(User user) {
        return dao.addUser(user)== 0 ? false : true;
    }

    @Override
    public boolean logoutUser(int userId) {
        return dao.logoutUser(userId)== 0 ? false : true;
    }

    @Override
    public boolean startUser(int userId) {
        return dao.startUser(userId)== 0 ? false : true;
    }

}
